package com.data.service.report.impl;

import com.data.domain.report.*;
import com.data.mapper.report.BatchdetailMapper;
import com.data.mapper.report.PrtryinfoMapper;
import com.data.mapper.report.ext.BatchdetailExtMapper;
import com.data.model.ReportModel;
import com.data.service.report.BatchDetailService;
import com.data.utill.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by silenceholic on 2017/5/2.
 */
@Service(value = "batchDetailService")
public class BatchDetailServiceImpl implements BatchDetailService{

    @Autowired
    private BatchdetailMapper batchdetailMapper ;

    @Autowired
    private PrtryinfoMapper prtryinfoMapper;

    @Autowired
    private BatchdetailExtMapper batchdetailExtMapper;

    /**
     * 基本的带分页的查询语句
     * @param reportModel
     * @return
     */
    @Override
    public List<Batchdetail> getAllBatchDetail(ReportModel reportModel){
        BatchdetailCriteria batchdetailCriteria = new BatchdetailCriteria();
        BatchdetailCriteria.Criteria criteria=batchdetailCriteria.createCriteria();
        if (StringUtils.isNoNullString(reportModel.getBegin())&&StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateBetween(reportModel.getBegin(),reportModel.getEnd());
        }
        if (StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //入网单位代码
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getOrgcode()))){
            criteria.andOrgcodeEqualTo(StringUtils.getIdFromString(reportModel.getOrgcode()));
        }
        //业务类型
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getPrtry()))){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //协议号
        if (StringUtils.isNoNullString(reportModel.getAgtid())){
            criteria.andAgtidLike(reportModel.getAgtid()+"%");
        }
        //计量对象用户号
        if (StringUtils.isNoNullString(reportModel.getBizid())){
            criteria.andBizidLike(reportModel.getBizid()+"%");
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }
        List<Batchdetail> data = new ArrayList<>();

        batchdetailCriteria.setOrderByClause("workdate desc");

        RowBounds rowBounds = new RowBounds();
        if (StringUtils.isNoNullString(reportModel.getStart()) && StringUtils.isNoNullString(reportModel.getLength())){
//            batchdetailCriteria.setStart();
            rowBounds = new RowBounds(Integer.parseInt(reportModel.getStart()),Integer.parseInt(reportModel.getLength()));
        }

        for (Batchdetail batchdetail:batchdetailMapper.selectByExampleWithRowbounds(batchdetailCriteria,rowBounds)){
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(batchdetail.getPrtry());
            batchdetail.setPrtry(prtryinfoMapper.selectByExample(prtryinfoCriteria).get(0).getName());
            data.add(batchdetail);
        }
        return data;
    }

    /**
     * 基本的count
     * @param reportModel
     * @return
     */
    @Override
    public int countBatchDetail(ReportModel reportModel){
        BatchdetailCriteria batchdetailCriteria = new BatchdetailCriteria();
        BatchdetailCriteria.Criteria criteria=batchdetailCriteria.createCriteria();
        if (StringUtils.isNoNullString(reportModel.getBegin())&&StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateBetween(reportModel.getBegin(),reportModel.getEnd());
        }
        if (StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //入网单位代码
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getOrgcode()))){
            criteria.andOrgcodeEqualTo(StringUtils.getIdFromString(reportModel.getOrgcode()));
        }
        //业务类型
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getPrtry()))){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //协议号
        if (StringUtils.isNoNullString(reportModel.getAgtid())){
            criteria.andAgtidLike(reportModel.getAgtid()+"%");
        }
        //计量对象用户号
        if (StringUtils.isNoNullString(reportModel.getBizid())){
            criteria.andBizidLike(reportModel.getBizid()+"%");
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }

        return batchdetailMapper.countByExample(batchdetailCriteria);
    }


    /**
     * 批量业务物理分页
     * @param reportModel
     * @return
     */
    public List<Batchdetail> selectBatchdetailByPage(ReportModel reportModel){
        String begin=StringUtils.cutDateStr(reportModel.getBegin());
        String senddate=StringUtils.cutDateStr(reportModel.getSenddate());
        String prtry=StringUtils.getIdFromString(reportModel.getPrtry());
        String orgcode=StringUtils.getIdFromString(reportModel.getOrgcode());
        List<Batchdetail> data = new ArrayList<>();
        List<Batchdetail> batchdetails = batchdetailExtMapper.selectBatchdetail(begin,reportModel.getEnd(),reportModel.getRefid(),prtry,
                senddate,reportModel.getAgtid(),reportModel.getErrcode(),orgcode,reportModel.getBizid(),reportModel.getStart(),reportModel.getLength());
        for (Batchdetail batchdetail:batchdetails){
            String ptrcode = batchdetail.getPrtry();
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(ptrcode);
            List<Prtryinfo> prtryinfos = prtryinfoMapper.selectByExample(prtryinfoCriteria);
            if (null!=prtryinfos&&prtryinfos.size()>0) {
                batchdetail.setPrtry(prtryinfos.get(0).getName());
            }
            data.add(batchdetail);
        }
        return data;
    }
    /**
     * 用发起批次号取得批量业务明细
     * @param sendbatchno
     * @return
     */
    public Batchdetail getBatchdetailBySendBatchNo(String sendbatchno){
        BatchdetailCriteria batchdetailCriteria = new BatchdetailCriteria();
        BatchdetailCriteria.Criteria criteria=batchdetailCriteria.createCriteria();
        criteria.andSendbatchnoEqualTo(Integer.parseInt(sendbatchno));
        List<Batchdetail> list=batchdetailMapper.selectByExample(batchdetailCriteria);
        if(list.size()>=1){
            return list.get(0);
        }else {
            return null;
        }
    }

}
